package edu.olin.scheduler.gui.schedule;

import edu.olin.scheduler.common.*;
import edu.olin.scheduler.gui.*;
import java.awt.Color;
import java.awt.Graphics;

public class LineDrawer {
    
    public static void drawLines(Graphics g, int xmin, int xmax, int ymin, int ymax){
        drawLines(g,xmin,xmax,ymin,ymax,true,null);
        }
    
    public static void drawLines(Graphics g, int xmin, int xmax, int ymin, int ymax, boolean drawMinor){
        drawLines(g,xmin,xmax,ymin,ymax,drawMinor,null);
        }
    
    public static void drawLines(Graphics g, int xmin, int xmax, int ymin, int ymax, boolean drawMinor, Color c){
        for(int i=GUIPrefs.getStart().hours(); i<GUIPrefs.getEnd().hours(); i++){
            int ymajor = getYPosition(new SimpleTime(i,0),ymin,ymax);
            int yminor = getYPosition(new SimpleTime(i,30),ymin,ymax);
            g.setColor( c!=null ? c : GUIPrefs.getHorizMajorColor() );
            g.drawLine(xmin,ymajor,xmax,ymajor);
            if(drawMinor){
                g.setColor( c!=null ? c : GUIPrefs.getHorizMinorColor() );
                g.drawLine(xmin,yminor,xmax,yminor);
                }
            }
        }

    //at what y-position is the given SimpleTime?
    public static int getYPosition(SimpleTime st, int ymin, int ymax){
        if(st==null){ throw new IllegalArgumentException("Cannot give location for null time"); }
        SimpleTime start = GUIPrefs.getStart();
        SimpleTime end = GUIPrefs.getEnd();
        int range = (end.minutesSinceMidnight()-start.minutesSinceMidnight());
        int delta = (st.minutesSinceMidnight()-start.minutesSinceMidnight());
        int height = ymax-ymin; //available space
        int y = ymin + (height*delta)/range;
        if(y<ymin){ y = ymin; }
        if(y>ymax){ y = ymax; }
        return y;
        }
    
    }
